--
-- Author: yawei.zhu
-- Date: 2018-09-26 15:43:27
--
-- SummonBossView
--
local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
local string_split = string.split

ClassRef.VIEW_ROOT_RES = Res.SummonBossScene

function ClassRef:init()
    -- 点击空白区域关闭
    self._ui.bg:setTouchEnabled(true)
    display.uiAddClick(self._ui.bg, function()
        self:closeView()
    end)

    self._ui.Button_close:addClickEventListener(function()
        self:closeView()
    end)

	self._ui.Button_team:addClickEventListener(function()
        executeFunc(self._onOperate, "gotoTeam")
		self:closeView()
	end)
    
	self._ui.Button_queding:addClickEventListener(function()
		local name = self._ui["Button_queding/name"]:getString()
		if name == L("ui_csd_853") then 
			-- 追踪, 在目标点200范围内随机一个位置
			local targetX = tonumber(self._tickInfo.position)
			targetX = math.floor(math.random(targetX - 199, targetX + 200))
			executeFunc(self._onOperate, "onTrackWildBoss", {tonumber(self._tickInfo.mapID), targetX})
		elseif name == L("lua_code_text_171") then
			executeFunc(self._onOperate, "reqSummonBoss", tonumber(self._tickInfo.ID))
		end
		self:closeView()
	end)

	--self._ui.tlt:setString(L("召唤卷轴（等级30）"))
	self._ui.tlt_2:setString(L("lua_code_text_248"))
	self._ui.tips:setString(L("lua_code_text_81"))

	self._targetName = self._ui.tlt_1
end

function ClassRef:updateView(tickInfo)
    if not tickInfo then return end
    self._tickInfo = tickInfo

    -- 是否到达指定的位置
    local bArrived = false
    if tonumber(tickInfo.mapID) == MapManager:getMapId() then
        local posX = FieldManager:getCharacterX()
        local positionX = tonumber(tickInfo.position)
        if math.abs(posX - positionX) <= 200 then -- 200范围以内
            bArrived = true
        end
    end
    -- 按钮名字
    if bArrived then
    	self._ui["Button_queding/name"]:setString(L("lua_code_text_171"))
    else
    	self._ui["Button_queding/name"]:setString(L("ui_csd_853"))
    end
    -- 战力
    local curPower = app:getInst("TeamModel"):getTeamTotalCapacity()
    if curPower == 0 then
    	curPower = UD.player.combatPower
    end
    self._ui.num:setString(curPower .. "/" .. tickInfo.powerNeed)
    if tonumber(curPower) < tonumber(tickInfo.powerNeed) then
        self._ui.num:setTextColor(cc.c3b(255, 105, 85))
    else
        self._ui.num:setTextColor(cc.c3b(255, 255, 255))
    end
    -- 目标
    self._ui.tlt:setString(string.format(L("lua_code_text_98"), L(tickInfo.name), L(tickInfo.openLv)))
    self._ui.tlt_1:setString(L(tickInfo.description))
    -- 掉落
    local render = IconRender.create(self._ui.icon)
    local dropIdAry = string_split(tickInfo.drop, ";")
    local drops = UD:parseDropIDToBagItem(dropIdAry[1])
    render:setData(drops[1])
    render:setTipEnable(true)
end

return ClassRef
